#!/usr/bin/env python
# -*- coding: utf-8 -*-
#   
#       Copyright 2010 Santiago Lezica, Juan Pablo Orsay, Facundo Martinez.
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

# Abstract Base Class library:
from inspect import getmembers, ismethod
from abc import ABCMeta, abstractproperty, abstractmethod
    
class Message:
    type_id = "NIL0"    # every message subclass should override these two.

    def vars(self):
        return [name for (name, value)
                in getmembers(self.__class__, lambda m: not ismethod(m))
                if not name.startswith("_")]

    def loadd(self, dictionary):
       
        for k in filter(lambda k: k in self.vars(), dictionary):
            setattr(self, k, dictionary[k])

    def dumpd(self):
        return dict([ (v, getattr(self, v)) for v in self.vars() ])

class Handler:
    __metaclass__ = ABCMeta

    handle_type_id = None # every handler subclass should override this.
    
    @abstractmethod
    def handle(self, data): # and this, obviously.
        pass

class Codec:
    __metaclass__ = ABCMeta

    @abstractmethod
    def encode(self, obj):
        pass

    @abstractmethod
    def decode(self, string):
        pass

class CommObject:
    __metaclass__ = ABCMeta

    @abstractmethod
    def config(self, handler, codec):
        pass
    
    @abstractmethod
    def sendData(self, id, data):
        pass
